package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.Product;
import com.wanmait.computershop.vo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

public class WebFavoritesDao {
    //根据用户ID查询收藏商品
    public List<Product> findFavorByID(Integer uid){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT b.* \n" +
                "FROM favor a , product b\n" +
                "WHERE a.pid = b.id and\n" +
                "a.uid=?";        
        try {
            List<Product> productList = queryRunner.query(sql,new BeanListHandler<>(Product.class),uid);
            return productList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    //添加收藏
    public void addFavorByID(Integer uid,Integer pid){       
        
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "INSERT INTO favor (uid, pid) VALUES (?,?);";

        try {
            queryRunner.update(sql,uid,pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //删除收藏
    public void delFavorByID(Integer uid,Integer pid){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "DELETE FROM favor WHERE uid = ? AND pid = ?;";

        try {
            queryRunner.update(sql,uid,pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
